package com.RecipeBook.cooking;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.RecipeBook.browse.DataBaseHelper;
import com.RecipeBook.common.Recipe;

import com.RecipeBook.common.RecipeActivity;
import com.RecipeBook.common.RecipeInProgress;
import com.RecipeBook.main.GlobalsManager;
import com.RecipeBook.main.R;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.AdapterView.OnItemClickListener;

/**
 * Main cooking class. Steps the user through a recipe. May support more
 * than one recipe at a time.
 */
public class CookingMain extends RecipeActivity {

	private class RIPAdapter extends ArrayAdapter<RecipeInProgress> {

		private ArrayList<RecipeInProgress> recipes;

		public RIPAdapter(Context context, int textViewResourceId,
				List<RecipeInProgress> objects) {
			super(context, textViewResourceId, objects);
			recipes = new ArrayList<RecipeInProgress>(objects);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.rip_list_item, null);
			}
			RecipeInProgress rip = recipes.get(position);
			if (rip != null) {
				Recipe r = rip.getRecipe();
				TextView name = (TextView) v.findViewById(R.id.TextView01);
				TextView progress = (TextView) v.findViewById(R.id.TextView02);
				ImageView picture = (ImageView) v.findViewById(R.id.ImageView01);
				if (name != null) {
					name.setText(r.getName());
				}
				if(progress != null){
					progress.setText("Progress: "+rip.getCurrentStep()+"/"+rip.getTotalSteps());
				}
				if(picture != null){
					//picture.setImageDrawable(Drawable.createFromPath(r.getImageFile()));
					try {
						Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(r.getImageFile()).getContent());
						picture.setImageBitmap(bitmap); 
					} catch (MalformedURLException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			return v;
		}
	}
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cooking);
		
		DataBaseHelper dbh = new DataBaseHelper(this);
		dbh.openDataBase();
		
		ArrayList<RecipeInProgress> ripl = GlobalsManager.getGlobals().getRecipesInProgress();
		ListView lv=(ListView)findViewById(R.id.ListView01);
		
		if (ripl.isEmpty())
		{
			//ripl.add(new RecipeInProgress(dbh.getRecipeFromId(5)));
			lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,new String[]{"No recipes in progress"}));
		}
		else
		{
			lv.setAdapter(new RIPAdapter(this, android.R.layout.simple_list_item_1, ripl));
			lv.setOnItemClickListener(itemClickListener);
		}
	}
	
	public void onResume() {
		super.onResume();
		ListView lv=(ListView)findViewById(R.id.ListView01);
		((ArrayAdapter<RecipeInProgress>)lv.getAdapter()).notifyDataSetChanged();
	}
	
	
	// LISTENERS -----------------------------------------------------

	private OnItemClickListener itemClickListener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> av, View v, int position, long id) {
			Intent i = new Intent(v.getContext(), CookingRecipe.class);
			i.putExtra("index", position);
			startActivity(i);
		}
	};
	
	}


